package labox.innovation.gameserver.model.actor.instance;

import labox.innovation.config.Config;
import labox.innovation.gameserver.datatables.TeleportLocationTable;
import labox.innovation.gameserver.model.FuzeTeleportLocation;
import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.Scene;
import labox.innovation.gameserver.model.actor.L2Npc;
import labox.innovation.gameserver.network.serverpackets.ActionFailed;
import labox.innovation.gameserver.templates.chars.NpcTemplate;

/**
 * 传送师对象
 * 
 */
public final class FuzeWaypointInstance extends L2Npc {

	/**
	 * 所有的传送情况都失败
	 */
	private static final int COND_ALL_FALSE = 0;

	/**
	 * 拥有这个传送师
	 */
	private static final int COND_OWNER = 2;

	/**
	 * 通常的传送情况
	 */
	private static final int COND_REGULAR = 3;

	/**
	 * @param template
	 */
	public FuzeWaypointInstance(long objectId, NpcTemplate template) {
		super(objectId, template);
	}

	@Override
	public void onAction(FuzePcInstance player) {
		int condition = validateCondition(player);

		int whereTo = getTemplate()._id;
		if (condition == COND_REGULAR) {
			doTeleport(player, whereTo);
		}

	}

	private void doTeleport(FuzePcInstance player, int val) {
		FuzeTeleportLocation list = TeleportLocationTable.getInstance().getTemplate(val);
		if (list != null) {
			if (player.isDead())
				return;
			if (_log.isDebugEnabled())
				_log.debug("Teleporting player " + player.getName() + " to new location: " + list.getLocX() + ":" + list.getLocY(), Config.SERVER_ID, "跳转点");

			Scene scn = FuzeWorld.getInstance().getScene(list.getSceneId());

			if (scn == null) {
				_log.error("doTeleport scene id:" + list.getSceneId() + " is invaild", Config.SERVER_ID);
				player.sendPacket(ActionFailed.STATIC_PACKET);
				return;
			}

			if (scn.isActive()) {//如果场景已经激活
				player.teleToLocation(list.getSceneId(), list.getLocX(), list.getLocY());
			} else {//场景跳转失败
				player.sendPacket(ActionFailed.STATIC_PACKET);
			}

		} else
			_log.warn("No teleport destination with id:" + val, Config.SERVER_ID, "跳转点");
	}

	private int validateCondition(FuzePcInstance player) {
		return COND_REGULAR; // Regular access
	}
}
